<?

class GroupController extends Controller_Action
{
	
	public function preDispatch() 
	{
		$user = $this->getVkUser();
		if (!$user) {
			$this->_redirect('/auth');
		}
        
		parent::preDispatch();
	}
	
	public function editAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Группа не указана');
		}
		
		$select = GroupsPeer::getInstance()->select()->where('vk_id = ?', $this->_getParam('id'));
		$row = GroupsPeer::getInstance()->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Группа не найдена');
		}
		
		$watermark = '/images/groups/' . $row->vk_id . '/watermark.png';
		if(!is_file($_SERVER['DOCUMENT_ROOT'] . $watermark)){
			$watermark = 'none';
		}
		
		$select = SpamDetectedPeer::getInstance()->select();
		$select->where('group_id = ?', $row->vk_id);
		$select->order('id desc');
		$select->limit(100);
		$rowset = SpamDetectedPeer::getInstance()->fetchAll($select);
		$this->view->spams = $rowset;
		
		$this->view->group = $row;
		$this->view->watermark = $watermark;
	}
	
	public function watermarkUploadAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Группа не указана');
		}
		$this->_disableView();
		$this->_log->info('Edit group: upload watermark ' . $this->_log->array_render($_FILES));
		if(is_uploaded_file($_FILES["filename"]["tmp_name"])){
			move_uploaded_file($_FILES["filename"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/images/groups/'.$this->_getParam('id').'/watermark.png');
			$this->_log->info('Edit group: upload watermark final ' .  $_SERVER['DOCUMENT_ROOT'] . '/images/groups/'.$this->_getParam('id').'/watermark.png');
		}
		$this->_redirect('/group/edit?id=' . $this->_getParam('id'));
	}
}
